
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.logging.Logger;



public class Client {
    public static Logger logger = Logger.getLogger("Server");
    private static volatile boolean isFlag = true;
    private static final int PORT = 8080;
    private static ServerSocket serverSocket;
    private static HashMap<String,String> mappings;
    public static void main(String args[]) throws IOException {
        initMappings();
        serverSocket = initServerSocket();
        logger.info("服务器正式启动，http://localhost:8080");
        while (isFlag) {
            Socket socket = serverSocket.accept();
            // Task task=new Task(socket);
            // Thread thread=new Thread(task);
            // thread.start();
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            
            StringBuffer sb=new StringBuffer();
            String line;
            while((line=input.readLine())!=null){
                sb.append(line).append("\r\n");
            }
            // if((line=input.readLine())!=null){
            //     sb.append(line);
            // }

            System.out.println(sb.toString());
            

            // 向客户端回复信息
            PrintStream out = new PrintStream(socket.getOutputStream());
            String str = "HTTP/1.1 200 OK\r\n" + "Server: Tengine\r\n" + "Date: Sun, 06 May 2018 08:22:10 GMT\r\n"
                    + "Content-Type: application/json;charset=UTF-8\r\n" + "Connection: close\r\n"
                    + "X-Powered-By: ring/1.0.0\r\n" + "gsid: 010185222147152559493030300162313551811\r\n"
                    + "sc: 0.013\r\n" + "Access-Control-Allow-Origin: *\r\n" + "Access-Control-Allow-Methods: *\r\n"
                    + "Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,key,x-biz,x-info,platinfo,encr,enginever,gzipped,poiid\n"
                    + "\r\n" + "\r\n"
                    + "{\"status\":\"1\",\"count\":\"2\",\"info\":\"OK\",\"infocode\":\"10000\",\"lives\":[{\"province\":\"湖南\",\"city\":\"长沙市\",\"adcode\":\"430100\",\"weather\":\"阵雨\",\"temperature\":\"25\",\"winddirection\":\"东北\",\"windpower\":\"7\",\"humidity\":\"78\",\"reporttime\":\"2018-05-06 16:00:00\"},{\"province\":\"湖南\",\"city\":\"长沙县\",\"adcode\":\"430121\",\"weather\":\"阵雨\",\"temperature\":\"25\",\"winddirection\":\"东北\",\"windpower\":\"7\",\"humidity\":\"78\",\"reporttime\":\"2018-05-06 16:00:00\"}]}";
            String str1 = "HTTP/1.1 200 OK\r\n" + "Conten-Type:text/html;charset=UTF-8\r\n" + "\r\n";
            // 获取当前项目的路径C:\\Users\\Zhy\Desktop\\untitled1
            File directory = new File(".");
            directory.getCanonicalPath();
            System.out.println(directory.getCanonicalPath());
            File file = new File(directory.getCanonicalPath() + "\\src\\main\\resources\\templates", "index.html");
            StringBuffer sb1 = new StringBuffer();
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line1;
            while ((line1 = br.readLine()) != null) {
                sb1.append(line1).append(str);
            }
            // br.close();

            System.out.println(file.exists());
            // out.println(str1+sb1.toString());
            // System.out.println(str1+sb.toString());
            // out.print(str);
            out.print(str);
            out.close();
            input.close();
        }
        serverSocket.close();
        logger.info("服务器关闭");
    }

    public static ServerSocket initServerSocket() throws IOException {
        final ServerSocket serverSocket = new ServerSocket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress(PORT));
        return serverSocket;
    }
    public static void initMappings(){
        mappings.put("/","com.dmzc.http.RootServlet");
        //后期替换成properties文件
    }

    public static void shutdown() {
        isFlag=true;
    }
}
